package model

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"strings"
)

var _ UserEstateAuthModel = (*customUserEstateAuthModel)(nil)

type (
	// UserEstateAuthModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserEstateAuthModel.
	UserEstateAuthModel interface {
		userEstateAuthModel
		InsertBatch(ctx context.Context, auths []*UserEstateAuth) error
	}

	customUserEstateAuthModel struct {
		*defaultUserEstateAuthModel
	}
)

func (c *customUserEstateAuthModel) InsertBatch(ctx context.Context, auths []*UserEstateAuth) error {
	query := fmt.Sprintf("INSERT INTO %s (%s) VALUES ", c.table, userEstateAuthRowsExpectAutoSet)

	var values []interface{}
	var valueStrings []string

	for _, auth := range auths {
		valueStrings = append(valueStrings, "(?, ?, ?)")
		values = append(values, auth.UserId, auth.EstateId, auth.Status)
	}
	query += strings.Join(valueStrings, ",")

	_, err := c.ExecNoCacheCtx(ctx, query, values...)
	return err
}

// NewUserEstateAuthModel returns a model for the database table.
func NewUserEstateAuthModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UserEstateAuthModel {
	return &customUserEstateAuthModel{
		defaultUserEstateAuthModel: newUserEstateAuthModel(conn, c, opts...),
	}
}
